﻿using Aker.DevTeam;
using Aker.Product;
using System;
using System.Data;
using System.Windows.Forms;

namespace Aker.Uretim
{
    public partial class ImalatKartlari : XForm
    {
        int ProductId;

        DataTable dtProduct;

        public ImalatKartlari()
        {
            InitializeComponent();
        }

        public ImalatKartlari(int ProductId)
        {
            InitializeComponent();
            this.ProductId = ProductId;
        }

        private void KumasImalatKarti_Load(object sender, EventArgs e)
        {
            //Yön tuşlarına basınca, TAB'a basmış gibi hareket etmesin diye eklendi.
            foreach (Control control in headerPanel.Controls)
                control.PreviewKeyDown += new PreviewKeyDownEventHandler(control_PreviewKeyDown);

            dtProduct = Data.DM.ExecuteDataTable("GetImalatKartlari");
            gleProduct.Properties.Assign(Utility.GetAutoCompGLE(dtProduct, "Article", "ProductId"));

            if (ProductId > 0)
                gleProduct.EditValue = ProductId;
            else
                gleProduct.EditValue = null;
        }

        void control_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == Keys.Left || e.KeyCode == Keys.Right)
                e.IsInputKey = true;
        }

        private void gleProduct_EditValueChanged(object sender, EventArgs e)
        {
            DataRow dr = (gleProduct.GetSelectedDataRow() as DataRowView).Row;
            ProductId = dr["ProductId"].ToIntNN();
            int StokKartiTipi = dr["StokKartiTipi"].ToIntNN();

            if (StokKartiTipi == 1)
            {
                ImalatReport20 hr = new ImalatReport20();
                hr.ArtikelRaporu(ProductId);
                reportPresenter.View(hr);
            }
            else if (StokKartiTipi == 2)
            {
                ImalatReport11 hr = new ImalatReport11();
                hr.ArtikelRaporu(ProductId);
                reportPresenter.View(hr);
            }
        }

        private void btnOnceki_Click(object sender, EventArgs e)
        {
            int index = gleProduct.Properties.GetIndexByKeyValue(ProductId);
            if (index == 0)
                gleProduct.EditValue = gleProduct.Properties.GetKeyValue(dtProduct.Rows.Count - 1);
            else
                gleProduct.EditValue = gleProduct.Properties.GetKeyValue(index - 1);
        }

        private void btnSonraki_Click(object sender, EventArgs e)
        {
            int index = gleProduct.Properties.GetIndexByKeyValue(ProductId);
            if (index == dtProduct.Rows.Count - 1)
                gleProduct.EditValue = gleProduct.Properties.GetKeyValue(0);
            else
                gleProduct.EditValue = gleProduct.Properties.GetKeyValue(index + 1);
        }

        private void KumasImalatKarti_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Left)
                btnOnceki.PerformClick();
            else if (e.KeyCode == Keys.Right)
                btnSonraki.PerformClick();
        }

        private void btnCikis_Click(object sender, EventArgs e)
        {
            DataRow dr = (gleProduct.GetSelectedDataRow() as DataRowView).Row;
            ProductId = dr["ProductId"].ToIntNN();
            AppSession.MainForm.ShowMdiChildForm(typeof(ImalatCikis), ProductId);
        }

        private void btnImalatKarti_Click(object sender, EventArgs e)
        {
            DataRow dr = (gleProduct.GetSelectedDataRow() as DataRowView).Row;
            int ProductId = dr["ProductId"].ToIntNN();
            int StokKartiTipi = dr["StokKartiTipi"].ToIntNN();

            if (StokKartiTipi == 1)
                AppSession.MainForm.ShowMdiChildForm(typeof(YeniImalatKarti1), ProductId);
            else if (StokKartiTipi == 2)
                AppSession.MainForm.ShowMdiChildForm(typeof(YeniImalatKarti2), ProductId);
        }
    }
}
